<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

// 表单数据
const loginForm = ref({
  username: '',
  password: ''
})

// 表单引用
const loginFormRef = ref(null)

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 路由实例
const router = useRouter()

// 登录处理
const handleLogin = async () => {
  try {
    // 发送登录请求到后端（通过代理）
    const response = await axios.post('/api/login', {
      username: loginForm.value.username,
      password: loginForm.value.password
    })

    if (response.data.code === 200) {
      ElMessage.success('登录成功')
      // 跳转到首页
      router.push('/')
    } else {
      ElMessage.error(response.data.message || '登录失败')
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    ElMessage.error('登录请求失败，请检查网络连接或服务器状态')
  }
}

// 重置表单
const resetForm = () => {
  loginForm.value.username = ''
  loginForm.value.password = ''
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">
        <h2>用户登录</h2>
      </div>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginFormRef"
        class="login-form"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>

        <el-form-item class="login-buttons">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="register-link">
        <el-button type="text" @click="goToRegister">没有账号？立即注册</el-button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;

  .login-box {
    width: 400px;
    padding: 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .login-title {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        color: #333;
        font-size: 24px;
        font-weight: bold;
      }
    }

    .login-form {
      .login-buttons {
        display: flex;
        justify-content: space-between;
        margin-top: 30px;

        :deep(.el-form-item__content) {
          justify-content: space-between;
        }

        .el-button {
          width: 45%;
        }
      }
    }

    .register-link {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>
